<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Register Src="~/Views/Menu/MainMenu.ascx" TagName="MainMenu" TagPrefix="mmuc" %>

<%@ Register Src="~/Views/Shared/LogOnUserControl.ascx" TagName="LogOnUserControl" TagPrefix="louc" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

 <script language="javascript" type="text/javascript">
     function ChangeStatus(id, name, status) {
         var s = 'active';
         if (status.toString() == "True") {
             s = 'deactive';
         }
         if (confirm("Are you sure that this document: " + name + " will be " + s + "?")) {
             // Perform change status.
             var action = '<%= Url.Action("ChangeStatus", "Doc") %>/' + id;
             var request = new Sys.Net.WebRequest();
             request.set_httpVerb("POST");
             request.set_url(action);
             request.add_completed(ChangeStatusCompleted);
             request.invoke();
         }
     }

     function ChangeStatusCompleted() {
         // Reload page  
         window.location.reload();
     }    
    </script>


<louc:LogOnUserControl ID="LogOnUserControl1" runat="server" />
   <!-- BEGIN: MainMenu -->
    <mmuc:MainMenu ID="MainMenu1" runat="server" />
    <!-- END: MainMenu -->
    <br />


    <h2>List All</h2>
    
    <%
        //if (Session["CommunityId"] == null)
        //{
        //    return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
        //}

        int communityId = Convert.ToInt32(ViewData["CommunityId"]);

        MVCWeb.Models.HOACRMDataContext dc = new MVCWeb.Models.HOACRMDataContext();
        
        IList<MVCWeb.Models.DocCategory> allCategories = dc.DocCategories.ToList<MVCWeb.Models.DocCategory>();
        IList<MVCWeb.Models.DocCategory> parentCategories = allCategories.Where(c => c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == null || c.DocCategoryId == 0).ToList<MVCWeb.Models.DocCategory>();
        IList<MVCWeb.Models.DocCategory> childCategories = allCategories.Where(c => c.ParentDocCategoryId > 0).ToList<MVCWeb.Models.DocCategory>();
        IList<MVCWeb.Models.Doc> allDocs = dc.Docs.Where(d => (dc.CommunityDocs.Where(cd => cd.CommunityId == communityId).Select(cd => cd.DocId).Contains((Int32?)(d.DocId)))).ToList<MVCWeb.Models.Doc>();
    %>
    
    
    <fieldset>
        <legend>Document categories </legend>
        <% foreach (var item in parentCategories)
           { %>
        <p>
            <b>
                <%= Html.ActionLink(Html.Encode(item.Category), "List", new { communityId = Request.QueryString["CommunityId"], categoryId = item.DocCategoryId })%></b>
                <%--(<%= Html.Encode(item.Docs.Count) %>)--%>
        </p>
        <% } %>
    </fieldset>
    
    
    
    
    
    <% foreach (var item in parentCategories)
       {
    %>
    
    <fieldset>
        <legend>
            <%= item.DocCategoryId == -1 ? "Documents have not been assigned to any category" : item.Category %>
        </legend>
        <br />
        
        
        
        
        
        
          <table id="document">
                    <tr>
                        <% if (HttpContext.Current.User.IsInRole("Administrators") || HttpContext.Current.User.IsInRole("PropertyManagers"))
                           { %>
                        <th>
                        </th>
                        <th>Status
                        </th>
                        <% } %>
                        <th>
                            Title
                        </th>
                        <th>
                            Modified Date
                        </th>
                        <th>
                            Size (Kb)
                        </th>
                        <th>
                        </th>
                    </tr>
                
                
                
                
                
                
                
                      
                <% foreach (var doc in allDocs.Where(d => d.DocCategoryId == item.DocCategoryId))
               { %>
               
               
               
               
               
               
               
               <tr>
                <% if (HttpContext.Current.User.IsInRole("Administrators") || HttpContext.Current.User.IsInRole("PropertyManagers"))
                   { %>
                <td>
                    <a href='<%= Url.Action("Edit", "Doc", new { id = doc.DocId })%>'>Edit </a>
                </td>
                <td>
                    <a onclick="ChangeStatus(<%=doc.DocId%>, '<%=doc.Name%>', '<%=doc.Active%>')" href="JavaScript:void(0)">
                    <%
                        if (doc.Active)
                        {
                    %>
                    Active
                    <%
                        }
                    else
                    {%>
                    Deactive
                    <%
                    }%>
                    </a>
                </td>
                <% } %>
                <td class="name">
                    <%= Html.Encode(doc.Name)%>
                </td>
                <td>
                    <%= Html.Encode(doc.ModifiedDate.Value.ToShortDateString())%>
                </td>
                <td>
                    <%= MVCWeb.Helpers.FileHelper.GetFileSize(doc.Size) %>
                </td>
                <td>
                    <a target="_blank" href='<%= doc.URLLink %>'>Download</a>
                </td>
            </tr>
               
               
               
               
               
               
               
               
               
                    
                <% } %>
                </table>
        
        
        
        
        
        
        
        
        
        
        
        
       
        
        
        
        <% foreach (var subCat in childCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId))
          { %>
          <div style="padding-left:20px; padding-right:10px;"> 
            <fieldset>
                <legend>
                <%= subCat.Category%>
                </legend>
                
                <br />
                
                
                
                
                
                
                
                <table id="document">
                    <tr>
                        <% if (HttpContext.Current.User.IsInRole("Administrators") || HttpContext.Current.User.IsInRole("PropertyManagers"))
                           { %>
                        <th>
                        </th>
                        <th>
                        </th>
                        <% } %>
                        <th>
                            Title
                        </th>
                        <th>
                            Modified Date
                        </th>
                        <th>
                            Size (Kb)
                        </th>
                        <th>
                        </th>
                    </tr>
                
                
                
                
                
                
                
                      
                <% foreach (var doc in allDocs.Where(d => d.DocCategoryId == subCat.DocCategoryId))
               { %>
               
               
               
               
               
               
               
               <tr>
                <% if (HttpContext.Current.User.IsInRole("Administrators") || HttpContext.Current.User.IsInRole("PropertyManagers"))
                   { %>
                <td>
                    <a href='<%= Url.Action("Edit", "Doc", new { id = doc.DocId })%>'>Edit </a>
                </td>
                <td>
                    <a onclick="ChangeStatus(<%=doc.DocId%>, '<%=doc.Name%>', '<%=doc.Active%>')" href="JavaScript:void(0)">
                    <%
                        if (doc.Active)
                        {
                    %>
                    Active
                    <%
                        }
                    else
                    {%>
                    Deactive
                    <%
                    }%>
                </a>
                </td>
                <% } %>
                <td class="name">
                    <%= Html.Encode(doc.Name)%>
                </td>
                <td>
                    <%= Html.Encode(doc.ModifiedDate.Value.ToShortDateString())%>
                </td>
                <td>
                    <%= MVCWeb.Helpers.FileHelper.GetFileSize(doc.Size) %>
                </td>
                <td>
                    <a target="_blank" href='<%= doc.URLLink %>'>Download</a>
                </td>
            </tr>
               
               
               
               
               
               
               
               
               
                    
                <% } %>
                </table>
            </fieldset>
          
          </div>
                
                
                
                
          <% } %>
        
        
    </fieldset>
    
    
    
    
        
       
        
    <% } %>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
